﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BaseSet.Data;
using BaseSet.Logic;
using BaseSet.Entity;
using BaseSet.Framework;
using BaseSet.Entity.Common;
using BaseSet.Entity.Common.Text;
using BaseSet;
using System.Data;


namespace WebSite.UserSetting
{
    public partial class MyFavorite : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                LoadContent(false);
            }
        }

        #region 加载内容
        private void LoadContent(bool mark)
        {
            //设置控件属性，从BasePage中得到
            PageConfig(BlogUPager);

            //List<Member> list = new List<Member();
            Users uinfo = UserBase.GetCurrentUser();
            
            //加载该用户收藏记录
            FavoriteHelper fhelper = new FavoriteHelper();
            Criteria cfavorite = new Criteria();
            cfavorite.Add(CriteriaType.Equals, "UsersID", uinfo.ID);
            cfavorite.Add(CriteriaType.Equals, "ObjectType", 1);

            Criteria c1 = new Criteria();
            c1.Mode = CriteriaMode.Or;
            List<Favorite> flist = fhelper.QueryFavoriteBySearch(cfavorite, new Order[] { new Order("CreateDate", OrderMode.Desc) });
            if (flist != null && flist.Count > 0)
            {
                for (int i = 0; i < flist.Count; i++)
                {
                    c1.AddOr(CriteriaType.Equals, "ID", flist[i].ObjectID);
                }

                BlogHelper bhelper = new BlogHelper();
                Criteria c = new Criteria();
                c.Add(CriteriaType.Equals, "Flag", 0);
                string title = txtTitle.Text.Trim();
                if (!string.IsNullOrEmpty(title))
                {
                    c.Add(CriteriaType.Like, "Title", "%" + title + "%");
                }
                c.Criterias.Add(c1);

                BlogUPager.ItemCount = bhelper.QueryBlogCountBySearch(c);

                // 以下的值从BasePage中得到
                BlogUPager.UrlFormat = UrlFormat;
                BlogUPager.PrefixText = GetPreFixText(BlogUPager);

                if (mark)
                {
                    PagerInit(BlogUPager);
                }

                string[] fields = new string[] { "ID", "BlogBigType", "BlogTypeName", "BlogTypeID", "Title", "BlogContent", "UsersID", "NickName", "PVCount", "ReplyCount", "RecommandCount", "HasPic", "Flag", "CreateDate" };

                DataTable dt = new DataTable();
                dt = bhelper.QueryBlogBySearchToTable(c, new Order[] { new Order("CreateDate", OrderMode.Desc) }, BlogUPager.Begin - 1, BlogUPager.Count, fields);

                dt.Columns.Add(new DataColumn("strHasPic"));
                dt.Columns.Add(new DataColumn("strVideoPic"));

                if (dt != null && dt.Rows.Count > 0)
                {
                    foreach (DataRow dr in dt.Rows)
                    {
                        //判断是否存在图片
                        string haspic = dr["haspic"] + "";
                        if (haspic == "1")
                        {
                            dr["strHasPic"] = "<img src=\"../images/smallpic.jpg\" alt=\"此创作包含图片\"/>";
                        }
                        else
                        {
                            dr["strHasPic"] = "";
                        }

                        string blogbigtype = dr["BlogBigType"] + "";
                        if (blogbigtype == "视频")
                        {
                            dr["strVideoPic"] = "<img src=\"../images/smallvideo.jpg\" alt=\"视频\"/>";
                        }
                        else if (blogbigtype.ToLower() == "mp3")
                        {
                            dr["strVideoPic"] = "<img src=\"../images/mp3.png\" alt=\"mp3\"/>";
                        }
                        else
                        {
                            dr["strVideoPic"] = "";
                        }
                    }
                    rptBlog.Visible = true;
                    rptBlog.DataSource = dt;
                    rptBlog.DataBind();
                    lblMessage.Visible = false;
                }
                else
                {
                    rptBlog.Visible = false;
                    lblMessage.Visible = true;
                    lblMessage.Text = "没有符合条件的收藏结果";
                }
            }
            else
            {
                lblMessage.Visible = true;
                lblMessage.Text = "还没有收藏任何创作";
            }
        }
        #endregion

        #region 列表事件
        protected void rptBlog_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            HiddenField hidID = (HiddenField)e.Item.FindControl("hidID");
            string id = hidID.Value;

            Users uinfo = UserBase.GetCurrentUser();

            if (e.CommandName == "delete")
            {
                FavoriteHelper fhelper = new FavoriteHelper();
                Criteria c = new Criteria();
                c.Add(CriteriaType.Equals, "ObjectID", id);
                c.Add(CriteriaType.Equals, "ObjectType", 1);
                c.Add(CriteriaType.Equals, "UsersID", uinfo.ID);
                List<Favorite> flist = fhelper.QueryFavoriteBySearch(c, null);
                Favorite finfo = flist[0];

                fhelper.DeleteFavorite(finfo);

                Tools.Alert("删除成功！");
            }
            LoadContent(false);
        }
        #endregion

        #region 搜索
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            SetCondition();
            LoadContent(true);
        }
        #endregion

        #region 查询条件设置
        private void SetCondition()
        {
            string title = txtTitle.Text.Trim();

            //将查询条件放入Session中
            Session["QueryList"] = title + "@" + "MyFavorite";

        }

        private void LoadCondition()
        {
            string condition = Session["QueryList"] + "";

            string[] conditionAll = condition.Split('@');
            if (conditionAll.Length == 2)
            {
                string page = conditionAll[1];
                if (page == "MyFavorite")
                {
                    txtTitle.Text = conditionAll[0];
                }
            }
        }
        #endregion
    }
}